package com.licheng.controller;


import com.licheng.anno.Log;
import com.licheng.pojo.Dept;
import com.licheng.pojo.Result;
import com.licheng.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j

public class DeptController {
    @Autowired
    private DeptService deptService;

    /**
     * 查询所有部门
     *
     * @return
     */

    @GetMapping("/depts")
    public Result list() {
        List<Dept> deptList = deptService.list();
        return Result.success(deptList);
    }

    /**
     * 根据id删除部门
     *
     * @param id 传来部门id
     * @return
     */
    @Log

    @DeleteMapping("/depts")
    public Result deleteDept(Integer id) {
        deptService.deleteDept(id);
        System.out.println("删除成功");
        return Result.success();
    }

    /**
     * 新增部门
     *
     * @param dept 接收名字来创建部门
     * @return
     */
    @Log
    @PostMapping("/depts")
    public Result insertDept(@RequestBody Dept dept) {
        deptService.insertDept(dept);
        return Result.success();
    }

    /**
     * 根据id查询部门
     *
     * @param id 接收id
     * @return
     */
    @GetMapping("/depts/{id}")
    public Result selectDeptById(@PathVariable Integer id) {

        Dept dept = deptService.selectDeptById(id);
        return Result.success(dept);
    }

    /**
     * 修改部门
     *
     * @param dept
     * @return
     */
    @Log
    @PutMapping("/depts")
    public Result saveDept(@RequestBody Dept dept) {
        deptService.updateDept(dept);


        return Result.success();
    }
}



